uniform sampler2D tex;
uniform sampler2D shadow;

varying vec4 ShadowCoord;


void
main ()
{
    vec4 color = texture2D(tex,gl_TexCoord[0].st);

    if (color==vec4(0,0,0,0))
        color=gl_Color;

    //else color*=gl_Color;

    vec4 shadowCoordinateWdivide = ShadowCoord / ShadowCoord.w ;

    // Used to lower moiré pattern and self-shadowing
    shadowCoordinateWdivide.z -= 0.0005;


    float distanceFromLight = texture2D(shadow,shadowCoordinateWdivide.st).z;
//float distanceFromLight=shadow2DProj(shadow, shadowCoordinateWdivide).r;

    float shadowFact = 1.0;
    if (ShadowCoord.w > 0.0)
        shadowFact = distanceFromLight < shadowCoordinateWdivide.z ? 0.8: 1.0 ;
   // gl_FragColor =clamp(color,0.0,1.0)*shadowFact;
    gl_FragColor =color*shadowFact;
 //gl_FragColor =gl_Color;

}

